/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.sys.web.goods;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.hk.sys.entity.goods.HkGoods;
import com.thinkgem.jeesite.modules.hk.sys.service.goods.HkGoodsService;

/**
 * 物品管理Controller
 * @author 罗天文
 * @version 2018-03-14
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/goods/hkGoods")
public class HkGoodsController extends BaseController {

	@Autowired
	private HkGoodsService hkGoodsService;
	
	@ModelAttribute
	public HkGoods get(@RequestParam(required=false) String id) {
		HkGoods entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = hkGoodsService.get(id);
		}
		if (entity == null){
			entity = new HkGoods();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:goods:hkGoods:view")
	@RequestMapping(value = {"list", ""})
	public String list(HkGoods hkGoods, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<HkGoods> page = hkGoodsService.findPage(new Page<HkGoods>(request, response), hkGoods); 
		model.addAttribute("page", page);
		return "hk/sys/goods/hkGoodsList";
	}

	@RequiresPermissions("sys:goods:hkGoods:view")
	@RequestMapping(value = "form")
	public String form(HkGoods hkGoods, Model model) {
		model.addAttribute("hkGoods", hkGoods);
		return "hk/sys/goods/hkGoodsForm";
	}

	@RequiresPermissions("sys:goods:hkGoods:edit")
	@RequestMapping(value = "save")
	public String save(HkGoods hkGoods, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, hkGoods)){
			return form(hkGoods, model);
		}
		hkGoodsService.save(hkGoods);
		addMessage(redirectAttributes, "保存物品成功");
		return "redirect:"+Global.getAdminPath()+"/sys/goods/hkGoods/?repage";
	}
	
	@RequiresPermissions("sys:goods:hkGoods:edit")
	@RequestMapping(value = "delete")
	public String delete(HkGoods hkGoods, RedirectAttributes redirectAttributes) {
		hkGoodsService.delete(hkGoods);
		addMessage(redirectAttributes, "删除物品成功");
		return "redirect:"+Global.getAdminPath()+"/sys/goods/hkGoods/?repage";
	}

}